﻿#region

using System;
using System.Windows.Forms;
using MidnightPeach.Components;
using MidnightPeach.Engine.Components;
using MidnightPeach.Engine.Packages;

#endregion

namespace MidnightPeach.Controls
{
    public partial class PackageControl : UserControl
    {
        public PackageControl()
        {
            InitializeComponent();

            bdsListItem.DataSource = ListItem.GetTemplateSourceTypes();
        }

        public string CurrentPath { get; private set; }

        public Package GetPackage()
        {
            EndEdit();

            return (Package) bdsPackage.DataSource;
        }

        public void New()
        {
            CurrentPath = null;

            OpenCore("Memopad.mp");
        }

        public void Open(string filePath)
        {
            CurrentPath = filePath;

            OpenCore(filePath);
        }

        private void OpenCore(string filePath)
        {
            // TODO : 한번만 실행하면 Package.Variables 를 제대로 불러오지 못하는 문제가 있어, 강제로 두 번 호출함. 이유 불명.
            bdsPackage.DataSource = LocalStorage.ReadObject<Package>(filePath);
            bdsPackage.DataSource = LocalStorage.ReadObject<Package>(filePath);
        }

        public void Save()
        {
            EndEdit();

            LocalStorage.WriteObject(CurrentPath, GetPackage());
        }

        public void SaveAsNew(string filePath)
        {
            CurrentPath = filePath;
            Save();
        }

        public string EdmxPath
        {
            get { return txtEdmxPath.Text; }
        }

        private void EndEdit()
        {
            bdsPackage.EndEdit();
            bdsTemplates.EndEdit();
            bdsVariable.EndEdit();
        }

        private void btnCopyTemplate_Click(object sender, EventArgs e)
        {
            GeneralUtility.CopyToClipboard(txtBody.Text);
        }

        private void dgvTemplate_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0 || e.ColumnIndex != 2)
                return;

            var template = dgvTemplate.Rows[e.RowIndex].DataBoundItem as Template;
            if (template == null)
                return;

            OnGenerateButtonClicked(template);
        }

        private void btnCopyGeneratedCode_Click(object sender, EventArgs e)
        {
            GeneralUtility.CopyToClipboard(txtGeneratedCode.Text);
        }

        #region GenerateButtonClicked event things for C# 3.0
        public event EventHandler<GenerateButtonClickedEventArgs> GenerateButtonClicked;

        protected virtual void OnGenerateButtonClicked(GenerateButtonClickedEventArgs e)
        {
            if (GenerateButtonClicked != null)
                GenerateButtonClicked(this, e);
        }

        protected virtual void OnGenerateButtonClicked(Template template)
        {
            OnGenerateButtonClicked(new GenerateButtonClickedEventArgs(template));
        }

        protected virtual GenerateButtonClickedEventArgs OnGenerateButtonClickedWithReturn(Template template)
        {
            GenerateButtonClickedEventArgs args = new GenerateButtonClickedEventArgs(template);
            OnGenerateButtonClicked(args);

            return args;
        }

        public class GenerateButtonClickedEventArgs : EventArgs
        {
            public Template Template { get; set; }

            public GenerateButtonClickedEventArgs()
            {
            }

            public GenerateButtonClickedEventArgs(Template template)
            {
                Template = template;
            }
        }
        #endregion
    }
}